/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxinline.manager.criar;

import br.com.foxinline.modelo.Usuario;
import br.com.foxinline.servico.UsuarioServico;
import java.io.IOException;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author laverson
 */
@ManagedBean
@SessionScoped
public class ManagerLayout implements Serializable {

    @EJB
    UsuarioServico usuarioServico;
    private String user;

    @PostConstruct
    public void init() {
        String usuario = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName();

        this.user = usuarioServico.getNomeUsuario(usuario);
        String[] nome = this.user.split(" ");
        if (nome.length > 0) {
            this.user = nome[nome.length - 1];
        }
    }

    public void logout() throws IOException {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.responseComplete();
        facesContext.getExternalContext().invalidateSession();
        facesContext.getExternalContext().redirect("/Paralelus-war/login.jsp");
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    /**
     * Retorna o usuário atualmente logado
     *
     * @return usuário logado
     */
    public Usuario getCurrentUser() {
        return usuarioServico.getCurrentUser();
    }
    
    public boolean isAdmin() {
        return usuarioServico.isAdmin();
    }
}
